package cn.harry.cabinet.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import cn.harry.cabinet.model.entity.User

/**
 * 用户表数据访问对象
 */
@Dao
interface UserDao {
    
    @Query("SELECT * FROM users ORDER BY createTime DESC")
    fun getAllUsers(): List<User>
    
    @Query("SELECT * FROM users ORDER BY createTime DESC LIMIT :pageSize OFFSET :offset")
    fun getUsersWithPagination(pageSize: Int, offset: Int): List<User>
    
    @Query("SELECT COUNT(*) FROM users")
    fun getUserCount(): Int
    
    @Query("SELECT * FROM users WHERE id = :id")
    fun getUserById(id: Long): User?
    
    @Query("SELECT * FROM users WHERE employeeId = :employeeId LIMIT 1")
    fun getUserByEmployeeId(employeeId: String): User?

    @Query("SELECT * FROM users WHERE featureCode = :featureCode LIMIT 1")
    fun getUserByFeatureCode(featureCode: String): User?
    
    @Query("SELECT * FROM users WHERE name LIKE '%' || :query || '%' OR employeeId LIKE '%' || :query || '%' OR phoneNumber LIKE '%' || :query || '%' ORDER BY createTime DESC")
    fun searchUsers(query: String): List<User>
    
    @Query("SELECT * FROM users WHERE name LIKE '%' || :query || '%' OR employeeId LIKE '%' || :query || '%' OR phoneNumber LIKE '%' || :query || '%' ORDER BY createTime DESC LIMIT :pageSize OFFSET :offset")
    fun searchUsersWithPagination(query: String, pageSize: Int, offset: Int): List<User>
    
    @Query("SELECT COUNT(*) FROM users WHERE name LIKE '%' || :query || '%' OR employeeId LIKE '%' || :query || '%' OR phoneNumber LIKE '%' || :query || '%'")
    fun getSearchUserCount(query: String): Int
    
    @Insert
    fun insertUser(user: User): Long
    
    @Update
    fun updateUser(user: User)
    
    @Delete
    fun deleteUser(user: User)
    
    @Query("DELETE FROM users")
    fun deleteAllUsers()
}